
let postUrl = 'http://121.43.153.61:9090';
let token = 'Bearer ' + window.GlobalToken


//获取弹窗
let window_bg = document.getElementsByClassName("window_bg")[0];

//点击弹窗外部可隐藏弹窗
window.onclick = function (event) {
  if (event.target == window_bg) {
    event.target.style.display = "none";
    localStorage.removeItem('shopId')
  }
};

function closeFun() {
  window_bg.style.display = "none";
  localStorage.removeItem('shopId')

  //可根据需要调整事件
}
function show() {
  window_bg.style.display = "block";

}
function getTab() {
  let tab_list = document.querySelector('.tab_list');
  let lis = tab_list.querySelectorAll('li');
  for (var i = 0; i < lis.length; i++) {
    lis[i].setAttribute('index', i);
    lis[i].onclick = function () {
      for (var i = 0; i < lis.length; i++) {
        lis[i].className = '';
      }
      this.className = 'red';
    }
  }

}

function gameType() {
  $.ajax({
    url: postUrl + `/api/v1/common/shop_type`,
    method: 'get',
    // 通过headers对象设置请求头
    headers: { 'Content-Type': 'application/json', 'Authorization': token },
    data: {},
    dataType: 'json',
    //async: true,
    success: function (res) {
      if (res.code === 200) {
        let list = res.data
        list.unshift({
          dictValue: 0, dictLabel: '全部'
        })
        let content = document.querySelector('.mine_left_tab')
        let listStr = ''
        list.forEach((item, index) => {
          listStr += `
            <li onclick="goPath(${item.dictValue},${index})">${item.dictLabel}</li>
     `
        })
        content.innerHTML = listStr
        let tab_list = document.querySelector('.mine_left_tab');
        let lis = tab_list.querySelectorAll('li');
        lis[0].className = 'active'
      }
    },
    error: function (e) {
    }
  })

}
function getList(value = '') {
  let data = {
    type: value
  }
  $.ajax({
    url: postUrl + `/api/v1/shop/list`,
    method: 'get',
    // 通过headers对象设置请求头
    headers: { 'Content-Type': 'application/json', 'Authorization': token },
    data,
    dataType: 'json',
    //async: true,
    success: function (res) {
      if (res.code === 200) {
        let list = res.data
        let content = document.querySelector('#footer')
        let listStr = ''
        list.forEach((item, index) => {
          listStr += `
             <div class="box" >
        <img src="${item.icon}" alt="" class="box_img" />
        <div class="name">${item.name}</div>
        <div class="pay">
          <div class="price">${item.coins}金币</div>
          <div style="display:none;">${item.id}</div>
          <div class="go" onclick="getPay(${item.coins},${item.id})">立即购买</div>
        </div>
      </div>`
        })
        content.innerHTML = listStr
      }
    },
    error: function (e) {
    }
  })
}


function getPay(coins, id) {
  console.log(id, coins)
  window_bg.style.display = "block";
  let window_text_t = document.querySelector('.window_text_t')
  window_text_t.innerHTML = `是否确认支付${coins}金币购买吗？`
  localStorage.setItem("shopId", id);
}
function goRecharge() {
  window.ndkLeadPayment()
  // window.open('http://121.43.153.61/client/index4.html')
}
function clean() {
  window_bg.style.display = "none";
}
function verify() {
  window_bg.style.display = "none";
  let shopId = localStorage.getItem("shopId");
  let data = {
    shopId
  }
  $.ajax({
    url: postUrl + `/api/v1/shop/buy`,
    method: 'post',
    // 通过headers对象设置请求头
    headers: { 'Content-Type': 'application/json', 'Authorization': token },
    data: JSON.stringify(data),
    dataType: 'json',
    //async: true,
    success: function (res) {
      if (res.code === 200) {
        window.ndkShopFinish()
        toast.show('购买成功',2000)  //2000是时间，2s后弹窗消失
        getInfo()
      } else {
        toast.show(res.msg,2000)  //2000是时间，2s后弹窗消失
      }
    },
    error: function (e) {
    }
  })
}

function goPath(value, index) {
  console.log(value, 'value')
  if(value === 0){
    value = ''
  }
  let tab_list = document.querySelector('.mine_left_tab');
  let lis = tab_list.querySelectorAll('li');
  for (var i = 0; i < lis.length; i++) {
    for (var i = 0; i < lis.length; i++) {
      lis[i].className = '';
    }
  }
  lis[index].className = 'active'
  getList(value)
}

function getInfo() {
  $.ajax({
    url: postUrl + "/api/v1/login/getInfo",
    method: 'get',
    // 通过headers对象设置请求头
    headers: { 'Content-Type': 'application/json', 'Authorization': token },
    data: {},
    dataType: 'json',
    //async: true,
    success: function (res) {
      if (res.code === 200) {
        let info = res.data.user
        let content = document.querySelector('.recharge_l')
        let listStr = ''
        listStr += ` <span class="l_text">剩余金币：</span>
              <span class="r_text">${info.coins}</span>`
        content.innerHTML = listStr
      }
    },
    error: function (e) {
    }
  })
}
getInfo()
getList()
getTab()
gameType()